﻿using IHateWorking.Model;
using IHateWorking.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;
using MenuItem = System.Windows.Controls.MenuItem;
using System.Windows.Media.Media3D;
using System.Windows.Controls;
using OfficeOpenXml;
using ActiproSoftware.Windows.Controls.DataGrid;
using System.Reflection;
//#JMJ Create_20241208

namespace IHateWorking.ViewModel
{
    /// <summary>
    /// 音乐清单的 ViewModel
    /// </summary>
    public class IntrDetailViewModel : INotifyPropertyChanged
    {
        public IntrDetailViewModel()
        {
            Instance = this;
            ViewLoadedCommand = new CommandBase(ViewLoaded);
            BtnCommand = new CommandBase(BtnClick);
            KeyDownCommand = new CommandBase(KeyDown);
        }

        #region 变量的通知机制
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
        #endregion 以上：变量的通知机制

        #region //////////////////////////////静态变量//////////////////////////////
        /// <summary>
        /// 静态接口
        /// </summary>
        public static IntrDetailViewModel Instance { get; set; }

        #endregion 以上：/////////////////////静态变量//////////////////////////////


        #region //////////////////////////////非静态变量////////////////////////////

        /// <summary>
        /// 界面加载完成 Command
        /// </summary>
        public CommandBase ViewLoadedCommand { get; set; }

        /// <summary>
        /// 按钮 Command
        /// </summary>
        public CommandBase BtnCommand { get; set; }

        /// <summary>
        /// 键盘按下 Command
        /// </summary>
        public CommandBase KeyDownCommand { get; set; }

        /// <summary>
        /// 窗体界面
        /// </summary>
        public IntrDetail IntrDetailView { get; set; }

        private SubState _intrDetailViewState = new SubState("IntrDetailViewState");
        /// <summary>
        /// 窗体尺寸状态
        /// </summary>
        public SubState IntrDetailViewState
        {
            get => _intrDetailViewState;
            set
            {
                if (_intrDetailViewState != value)
                {
                    _intrDetailViewState = value;
                    OnPropertyChanged();
                }
            }
        }

        private DependencyObject _latestClickUI = new DependencyObject();
        /// <summary>
        /// 最新的鼠标按下控件
        /// </summary>
        public DependencyObject LatestClickedUI
        {
            get => _latestClickUI;
            set
            {
                if (_latestClickUI != value)
                { _latestClickUI = value; }
            }
        }

        #endregion 以上：/////////////////////非静态变量////////////////////////////


        #region //////////////////////////////静态方法//////////////////////////////

        #endregion 以上：/////////////////////静态方法//////////////////////////////


        #region //////////////////////////////非静态方法////////////////////////////
        /// <summary>
        /// 界面加载完成事件
        /// </summary>
        /// <param name="view">窗体/用户控件</param>
        private void ViewLoaded(object view)
        {
            IntrDetailView = view as IntrDetail;

            IntrDetailView.SizeChanged += IntrDetailView_SizeChanged;
            IntrDetailView.PreviewMouseDown += IntrDetailView_PreviewMouseDown;
            IntrDetailView.MouseDown += IntrDetailView_MouseDown;
            IntrDetailView.MouseDoubleClick += IntrDetailView_MouseDoubleClick;
            IntrDetailView.Closed += IntrDetailView_Closed;

            RefreshIntrDetailViewHeight();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IntrDetailView_Closed(object sender, EventArgs e)
        {
            if (MusicListViewModel.Instance.Vis_IntrDetailView.StateMode > 0)
            { MusicListViewModel.Instance.Vis_IntrDetailView.StateMode = 0; }
        }

        /// <summary>
        /// 窗体尺寸变化事件
        /// </summary>
        /// <param name="sender">窗体</param>
        /// <param name="e"></param>
        private void IntrDetailView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            int state = Convert.ToInt32(IntrDetailView.WindowState);
            if (state != _intrDetailViewState.StateMode)
            { IntrDetailViewState.StateMode = Convert.ToInt32(IntrDetailView.WindowState); }
        }

        /// <summary>
        /// 按钮按下 Command 方法
        /// </summary>
        /// <param name="sender">触发的按钮</param>
        private void BtnClick(object sender)
        {
            string tag = "";
            if (sender is Button btn)
            { tag = btn.Tag == null ? "" : btn.Tag.ToString(); }

            switch (tag)
            {
                #region 窗体操作按钮
                case "IntrDetailViewClose":
                    { IntrDetailView.Close(); }
                    break;
                case "IntrDetailViewMinSize":
                    { IntrDetailView.WindowState = WindowState.Minimized; }
                    break;
                case "IntrDetailViewNormalSize":
                    { IntrDetailView.WindowState = WindowState.Normal; }
                    break;
                case "IntrDetailViewMaxSize":
                    { IntrDetailView.WindowState = WindowState.Maximized; }
                    break;
                #endregion 以上：窗体操作按钮
                default:
                    break;
            }
        }

        /// <summary>
        /// 主窗体的鼠标预按下的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void IntrDetailView_PreviewMouseDown(object sender, MouseEventArgs e)
        {
            if (e.OriginalSource is DependencyObject source && (e.OriginalSource is Visual || e.OriginalSource is Visual3D))
            { LatestClickedUI = source; }
        }

        /// <summary>
        /// 键盘按下 Command 方法
        /// </summary>
        /// <param name="sender">触发的界面</param>
        private void KeyDown(object sender)
        {
            //Esc = 退出
            if (Keyboard.IsKeyDown(Key.Escape))
            {
                if (MusicListViewModel.Instance.Vis_IntrDetailView.StateMode > 0)
                { MusicListViewModel.Instance.Vis_IntrDetailView.StateMode = 0; }
            }
        }

        /// <summary>
        /// 窗体拖拽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IntrDetailView_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            { IntrDetailView.DragMove(); }
        }

        /// <summary>
        /// 窗体双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IntrDetailView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                if (LatestClickedUI is null) { return; }

                //区分是主界面的什么控件触发的双击菜单
                object uIElm = MusicListViewModel.Instance.GetMyTypeUI(LatestClickedUI, typeof(DockPanel));
                if (uIElm != null && uIElm is DockPanel dP && dP.Name == "dP_Title")
                {
                    if (IntrDetailView.WindowState == WindowState.Maximized)
                    { IntrDetailView.WindowState = WindowState.Normal; }
                    else if (IntrDetailView.WindowState == WindowState.Normal)
                    { IntrDetailView.WindowState = WindowState.Maximized; }
                }
            }
        }

        /// <summary>
        /// 刷新介绍界面高度
        /// </summary>
        public void RefreshIntrDetailViewHeight()
        { IntrDetailView.Height = 23.77 * MusicListViewModel.Instance.Detail_Intr.Count + 55.83; }
        #endregion 以上：/////////////////////非静态方法////////////////////////////
    }
}
